﻿package classes.pet 
{
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.SharedObject;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	import com.mttang.event.CommonEvent;
	import com.mttang.event.JEvent;
	import com.mttang.common.InterfaceCommon;
	import classes.GameSetting;
	import classes.savedata.RockmanData;
	import classes.audio.Audio;
	import classes.audio.AudioRockman
	
	/**
	 * ...
	 * @author acydan
	 */
	public class Pet extends MovieClip implements InterfaceCommon
	{
		//pet property 
		//pet.skills      技能天赋
		//pet.figthStatus 战斗状态
		//pet.growthStatus成长状态
		
		private static const skillAddedNoArr : Array = [5,2,3,2,0,0,0];//所加技能的点数
		private static const skillTotalNoArr : Array = [5, 3, 5, 4, 5, 3, 1];//技能的上限点数
		private static const elephantLevel:String = "1";
		private static const dragonLevel:String = "2";
		private static const petsCoordinate:Array = [new Point(85,410),new Point(236,410),new Point(378,410)];//宠物坐标；		
		
		private var bgMc : BgMc;
		private var elephant : Elephant;
		private var backBtn : BackBtn;
		private var goBtn : GoBtn;
		private var feedBtn : FeedBtn;
		private var arrowMark : ArrowMark;
		private var dragon : Dragon;
		private var skillBtn : SkillBtn;
		private var skillControl_Mc : SkillControl_Mc;
		private var explainMC : ExplainMC;
		private var explainTxtMc:ExplainTxtMc;
		private var tipMc:TipMc;
		private var tipMc1:TipMc1;
		private var tipMc2:TipMc2;
		private var tipMc3:TipMc3;
		private var marker:Marker;
		
		private var petType : String;//宠物是属于哪个系的；
		private var btnSprite : Sprite;
		private var currentPetName : String;
		private var currentPet : * ;
		private var overPet : * ;//鼠标移动到上面的宠物；
		private var overPetNeedMoney:int;
		private var gameData:Object;//游戏的所有数据；
		private var testdata:Object;
		private var petsNameArr:Array;//玩家拥有的宠物名字；
		private var petMcArr:Array;//玩家拥有的宠物
		private var petLevel:Array;//存储玩家拥有的宠物的等级；
		private var petLevelBar:Array;//存储玩家宠物的等级条；
		private var petLevelExp:Array = GameSetting.petLevelExp;
		private var petObjectIndex:Array;//宠物在共享对象里的index
		
		public function rock_pause():void
		{
			
		}
		public function rock_play():void
		{
			
		}
		public function rock_close():void//关闭清除当前的场景;swf文件的声音，flv在这里清除;
		{
			trace("diao...............");
			while (this.numChildren)
			{
				this.removeChildAt(0);
			}
			AudioRockman.stop(Audio.FIGHT_MUSIC_ONE);
			bgMc = null;
			btnSprite = null;
			skillControl_Mc = null;
			arrowMark = null;	
			explainTxtMc = null;
			explainMC = null;
		}
		public function setData(_data:Object):void//所有的场景通过这个接口获取数据对象;
		{
			AudioRockman.play(Audio.FIGHT_MUSIC_ONE,true);
			gameData = _data;
			trace(gameData.pet[0].level,"[[[[[[[[[[[[[[[[[[[[");
			petsNameArr = new Array();
			petLevel = new Array();	
			petObjectIndex = new Array();
			for (var i:int = 0; i < gameData.pet.length; i++ )
			{
				if (gameData.pet[i].status == 1)
				{
					petsNameArr.push(gameData.pet[i].name.toString() + gameData.pet[i].petid.toString() + "_"+i.toString());
					petLevel.push(int(gameData.pet[i].level));
					petObjectIndex.push(i);
				}				
			}
			trace("pet name",petsNameArr,petLevel,petObjectIndex);
			//trace("testData",gameData.user.name);
			init();		
		}
		public function cfgSound(status:String):void//控制声音;
		{
			
		}
		public function Pet() 
		{
			
		}
		
		private function init(evt : Event = null ):void
		{
			bgMc = new BgMc();
			this.addChildAt(bgMc, numChildren);
			getData();
		}
		private function getData():void//获取SharedObject数据
		{
			addMcToStage();
		}
		private function addMcToStage():void
		{
			btnSprite = new Sprite();						
			bgMc.moneyTxt.text = gameData.user.money.toString();//money.toString();
			bgMc.levelTxt.text = gameData.user.level.toString();//level.toString();
			bgMc.scoresTxt.text = gameData.user.exp.toString();//scores.toString();			
			this.addChildAt(btnSprite, numChildren);			
			petMcArr = new Array();
			petLevelBar = new Array();
			var _tempLength:int = petsNameArr.length;
			if (_tempLength > 3)
			{
				_tempLength = 3;
			}
			for (var i:int = 0; i < _tempLength; i++ )
			{
				trace(petsNameArr[i].slice(0,petsNameArr[i].indexOf("_",0)),"****************");
				var _tmp:Class = getDefinitionByName(petsNameArr[i].slice(0,petsNameArr[i].indexOf("_",0))) as Class;				
				petMcArr[i] = new _tmp();
				petMcArr[i].mouseChildren = false;
				petMcArr[i].gotoAndStop(getPetGrowupStatus(int(petLevel[i])));//这里的帧数是宠物的成长形态；
				btnSprite.addChildAt(petMcArr[i], btnSprite.numChildren);
				petMcArr[i].x = petsCoordinate[i].x;
				petMcArr[i].y = petsCoordinate[i].y;// - petMcArr[i].height / 2;
				petMcArr[i].name = petsNameArr[i];
				petLevelBar[i] = new BarXPMc();
				petLevelBar[i].x = petMcArr[i].x;
				petLevelBar[i].y = 465//petMcArr[i].y + petMcArr[i].height / 2 + 30;
				petLevelBar[i].levelTxt.text = gameData.pet[petObjectIndex[i]].level.toString();
				btnSprite.addChildAt(petLevelBar[i],btnSprite.numChildren);
				trace(petMcArr[i].x,petMcArr[i].y);
			}
			backBtn = new BackBtn();
			backBtn.name = "BackBtn";
			backBtn.x = 461;
			backBtn.y = 414;
			goBtn = new GoBtn();
			goBtn.name = "GoBtn";
			goBtn.x = 536;
			goBtn.y = 363;
			feedBtn = new FeedBtn();
			feedBtn.name = "FeedBtn";
			feedBtn.x = 518;
			feedBtn.y = 310;
			skillBtn = new SkillBtn();
			skillBtn.x = 547;
			skillBtn.y = 245;
			skillBtn.name = "SkillBtn";
			btnSprite.buttonMode = true;
			btnSprite.addChildAt(feedBtn, btnSprite.numChildren);
			btnSprite.addChildAt(goBtn, btnSprite.numChildren);			
			btnSprite.addChildAt(backBtn, btnSprite.numChildren);
			btnSprite.addChildAt(skillBtn,btnSprite.numChildren);
			btnSprite.addEventListener(MouseEvent.CLICK, clickHandler);
			btnSprite.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			btnSprite.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}
		private function tipClickHandler(evt:MouseEvent):void
		{
			trace(evt.target.parent.name, "???????????????");
			switch(evt.target.parent.name)
			{
				case "tipMc":
					this.removeChild(tipMc);
					tipMc = null;
					break;
				case "tipMc1":
					this.removeChild(tipMc1);
					tipMc1 = null;
					break;
				case "tipMc2":
					this.removeChild(tipMc2);
					tipMc2 = null;
					break;
				case "tipMc3":
					this.removeChild(tipMc3);
					tipMc3 = null;
					break;
			}
			if(marker){
				this.removeChild(marker);
				marker = null;
			}
		}
		private function addTipBox():void
		{
			marker = new Marker();
			marker.x = stage.stageWidth / 2;
			marker.y = stage.stageHeight / 2;
			tipMc = new TipMc();
			tipMc.name = "tipMc";
			tipMc.x = stage.stageWidth / 2;
			tipMc.y = stage.stageHeight / 2;
			this.addChildAt(marker,numChildren);
			this.addChildAt(tipMc, numChildren);
			tipMc.sureBtn.addEventListener(MouseEvent.CLICK,tipClickHandler);
		}
		private function clickHandler(evt : MouseEvent):void
		{
			trace("kkll", evt.target.name);
			switch(evt.target.name)
			{
				case "FeedBtn":
					if (currentPet)
					{
						feedPet(currentPet);
					}else {
						addTipBox();
					}					
					break;
				case "BackBtn":
					//rock_close();
					this.dispatchEvent(new JEvent(CommonEvent.BACK_EVENT));
					break;
				case "GoBtn":
					//rock_close();
					this.dispatchEvent(new JEvent(CommonEvent.PLAY_GAME_EVENT));
					break;
				case "SkillBtn":									
					var _tempStatus:int = getPetGrowupStatus(petLevel[getPetLevel(currentPetName)]);//宠物成长状态
					if (currentPetName) 
					{						
						if (_tempStatus > 1)
						{
							petType = currentPetName;
							skillControl_Mc = new SkillControl_Mc();
							this.addChildAt(skillControl_Mc, numChildren);
							skillControl_Mc.setSkillType(petType,currentPet,gameData,getPetLevel(currentPetName),petObjectIndex);	
						}else {
							tipMc3 = new TipMc3();
							tipMc3.name = "tipMc3";
							tipMc3.x = stage.stageWidth / 2;
							tipMc3.y = stage.stageHeight / 2;
							marker = new Marker();
							marker.x = stage.stageWidth / 2;
							marker.y = stage.stageHeight / 2;
							this.addChildAt(marker,numChildren);
							this.addChildAt(tipMc3, numChildren);
							tipMc3.sureBtn.addEventListener(MouseEvent.CLICK,tipClickHandler);
						}												
					}else {
						addTipBox();				
					}
					break;
				default : 
					if (arrowMark)
					{
						this.removeChild(arrowMark);
						arrowMark = null;
					}
					currentPetName = evt.target.name;		
					currentPet = evt.target;
					arrowMark = new ArrowMark();
					arrowMark.name = "ArrowMark";
					arrowMark.x = evt.target.x;
					arrowMark.y = evt.target.y - arrowMark.height/2 - 60;
					this.addChildAt(arrowMark,numChildren);	
			}			
		}
		private function feedPet(_currentPet : Object):void
		{			
			var _tmpNextLevelMoney:Number;
			var _tempPetType:String = _currentPet.name.slice(0, _currentPet.name.indexOf("_", 0));			
			switch(_tempPetType)
			{
				case "Ice1":
				case "Fire1":
				case "Water1":
				case "Physics1":
				case "Wind1":
				case "Thunder1":
					_tmpNextLevelMoney = petLevelExp[int(petLevel[getPetLevel(_currentPet.name)])];
					break;
				case "Ice2":
				case "Fire2":
				case "Water2":
				case "Physics2":
				case "Wind2":
				case "Thunder2":
					_tmpNextLevelMoney = petLevelExp[int(petLevel[getPetLevel(_currentPet.name)])] * 2;
					break;
				case "Ice3":
				case "Fire3":
				case "Water3":
				case "Physics3":
				case "Wind3":
				case "Thunder3":
					_tmpNextLevelMoney = petLevelExp[int(petLevel[getPetLevel(_currentPet.name)])] * 3;
					break;
				default:
			}
			if(explainTxtMc){
				explainTxtMc.needMoneyTxt.text = _tmpNextLevelMoney.toString();	
			}
			if (_tmpNextLevelMoney < gameData.user.money)
			{
				if (petLevel[getPetLevel(_currentPet.name)] < GameSetting.petHighestLevel)
				{					
					var _tempMoney:Number = gameData.user.money - _tmpNextLevelMoney;
					gameData.user.money = _tempMoney;
					bgMc.moneyTxt.text = gameData.user.money.toString();
					petLevel[getPetLevel(_currentPet.name)]++;	
					trace(petLevel[getPetLevel(_currentPet.name)],"1231***********");
					_currentPet.gotoAndStop(getPetGrowupStatus(petLevel[getPetLevel(_currentPet.name)]));
					var _tempLevel:Number = petLevel[getPetLevel(_currentPet.name)];
					if(explainTxtMc){
						explainTxtMc.needMoneyTxt.text = _tmpNextLevelMoney.toString();
					}
					petLevelBar[getPetLevel(_currentPet.name)].levelTxt.text = _tempLevel;					
					trace(petObjectIndex[getPetLevel(_currentPet.name)],"宠物档");
					gameData.pet[petObjectIndex[getPetLevel(_currentPet.name)]].level = _tempLevel;
					gameData.pet[petObjectIndex[getPetLevel(_currentPet.name)]].skillcount++;					
					RockmanData.saveData(gameData);
				}else {
					if(explainTxtMc){
						explainTxtMc.needMoneyTxt.text = _tmpNextLevelMoney.toString();	
					}
					tipMc1 = new TipMc1();
					tipMc1.name = "tipMc1";
					tipMc1.x = stage.stageWidth / 2;
					tipMc1.y = stage.stageHeight / 2;
					marker = new Marker();
					marker.x = stage.stageWidth / 2;
					marker.y = stage.stageHeight / 2;
					this.addChildAt(marker,numChildren);	
					this.addChildAt(tipMc1,numChildren);
					tipMc1.sureBtn.addEventListener(MouseEvent.CLICK,tipClickHandler);
				}
			}else {
				if(explainTxtMc){
						explainTxtMc.needMoneyTxt.text = _tmpNextLevelMoney.toString();	
				}
				tipMc2 = new TipMc2();
				tipMc2.name = "tipMc2";
				tipMc2.x = stage.stageWidth / 2;
				tipMc2.y = stage.stageHeight / 2;
				marker = new Marker();
				marker.x = stage.stageWidth / 2;
				marker.y = stage.stageHeight / 2;
				this.addChildAt(marker,numChildren);	
				this.addChildAt(tipMc2,numChildren);
				tipMc2.sureBtn.addEventListener(MouseEvent.CLICK,tipClickHandler);
			}
		}
		private function overHandler(evt : MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);	
			for (var i:int = 0; i < petsNameArr.length;i++ )
			{
				if (evt.target.name == petsNameArr[i])
				{
					addFilters(evt.target);	
					addExplainMc(evt.target);
					trace(petsNameArr[i].slice(0, petsNameArr[i].indexOf("_", 0)),"UUUUUUUUU");
					var _tempPetType:String = petsNameArr[i].slice(0, petsNameArr[i].indexOf("_", 0));
					switch(_tempPetType)
					{
						case "Water1":				
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Water2":				
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Water3":				
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						case "Fire1":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Fire2":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Fire3":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						case "Ice1":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Ice2":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Ice3":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						case "Physics1":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Physics2":
							addExplainMc(evt.target);
							addFilters(evt.target);
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Physics3":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						case "Thunder1":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Thunder2":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Thunder3":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						case "Wind1":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])];
							break;
						case "Wind2":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*2;
							break;
						case "Wind3":
							overPetNeedMoney = petLevelExp[int(petLevel[getPetLevel(evt.target.name)])]*3;
							break;
						default : 
					}					
				}
			}			
		}
		private function addExplianSp(_obj:Object):void
		{
			explainMC = new ExplainMC();
			explainMC.name = "ExplainMC";
			this.addChildAt(explainMC, numChildren);
			trace(_obj.x, _obj.y, _obj.height, _obj.width,_obj.name);
			explainMC.gotoAndPlay("a");
			explainMC.addEventListener(Event.COMPLETE, compliteHandler);
			switch(_obj.x)
			{
				case petsCoordinate[0].x:
					explainMC.x = petsCoordinate[0].x;
					explainMC.y = _obj.y - _obj.height / 2;		
					explainMC.rotationY = -180;
					break;
				case petsCoordinate[1].x:
					explainMC.x = petsCoordinate[1].x;
					explainMC.y = _obj.y - _obj.height / 2;
					break;
				case petsCoordinate[2].x:
					explainMC.x = petsCoordinate[2].x;
					explainMC.y = _obj.y - _obj.height / 2;
					break;
			}		
		}
		private function addExplainMc(_obj:Object):void
		{
			overPet = _obj;	
			if (explainMC)
			{
				if (_obj.x == explainMC.x)
				{
					
				}else {
					if (explainTxtMc)
					{
						this.removeChild(explainTxtMc);
						explainTxtMc = null;
					}
					this.removeChild(explainMC);					
					explainMC = null;					
					addExplianSp(_obj);
				}
			}else {
				addExplianSp(_obj);
			}				
		}
		private function compliteHandler(evt:Event):void
		{
			if (explainTxtMc)
			{
				this.removeChild(explainTxtMc);
				explainTxtMc = null;
			}			
			explainTxtMc = new ExplainTxtMc();
			explainTxtMc.needMoneyTxt.text = overPetNeedMoney.toString();			
			explainTxtMc.y = explainMC.y - 86;
			if (evt.target.rotationY == -180)
			{
				explainTxtMc.x = explainMC.x - 15;
			}else {
				explainTxtMc.x = explainMC.x - explainTxtMc.width + 15;		
			}			
			this.addChildAt(explainTxtMc,numChildren);
		}
		private function closeHandler(evt:Event):void
		{
			if(explainTxtMc){
				this.removeChild(explainTxtMc);
				explainTxtMc = null;
			}
		}		

		private function outHandler(evt : MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);			
			deletFilters(evt.target);
			switch(evt.target.name)
			{
				case "Water1":					
				case "Water2":					
				case "Water3":					
				case "Fire1":					
				case "Fire2":					
				case "Fire3":					
				case "Ice1":					
				case "Ice2":					
				case "Ice3":					
				case "Physics1":					
				case "Physics2":					
				case "Physics3":					
				case "Thunder1":					
				case "Thunder2":					
				case "Thunder3":					
				case "Wind1":					
				case "Wind2":					
				case "Wind3":
					if (explainMC) 
					{
						//explainMC.gotoAndPlay("b");
						//explainMC.addEventListener(Event.CLOSE,closeHandler);
					}
				default :
			}
			
		}
		private function deletFilters(_obj : Object):void
		{
			_obj.filters = null;
		}
		private function addFilters(_obj : Object):void
		{
			var glow : GlowFilter = new GlowFilter();
			glow.color = 0xFFFFFF;
			glow.alpha = 1;
			glow.blurX = 25;
			glow.blurY = 25;
			glow.quality = BitmapFilterQuality.MEDIUM;
			_obj.filters = [glow];
		}
		private function getNextLevelNeedMoney(_tmp:int):int//计算升级下一个等级所需要的钱
		{
			var arr:Array = [5,5];
			var _temp:int;
			if(_tmp==1)
			{
				_temp = 390;
			}else{
				for(var i=2;i<_tmp+1;i++)
				{
					arr[i]=arr[i-1]+arr[i-2];
				}
				_temp = arr[_tmp]*70;
			}
			return _temp;
		}
		private function getPetLevel(_petname:String):int//获取宠物索引
		{
			var tempNo:int;
			for (var i:int = 0; i < petsNameArr.length; i++ )
			{
				if (_petname == petsNameArr[i])
				{
					tempNo = i;
					break;
				}
			}
			return tempNo;
		}
		private function getPetGrowupStatus(_level:int):int
		{
			var growupStatus:int;
			if (_level == 0)
			{
				growupStatus = 1;
			}
			if (_level > 0 && _level <= 5)
			{
				growupStatus = 2;
			}
			if (_level > 5 && _level <= 9)
			{
				growupStatus = 3;
			}
			if (_level > 9 && _level <= 14)
			{
				growupStatus = 4;
			}
			if (_level > 14 && _level <= 20)
			{
				growupStatus = 5;
			}
			if (_level > 20 && _level <= 26)
			{
				growupStatus = 6;
			}
			return growupStatus;
		}
	}
}